package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.app.error.report.AppErrorReportDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.AppErrorReportService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/app-error-report")
public class AppErrorReportController {
    private final AppErrorReportService appErrorReportService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @PostMapping("/send")
    public ResponseEntity<ApiResponse<Object>> send(@Valid @RequestBody AppErrorReportDto data) {
        MyClaims claims = requestIdentityExtractor.getClaims();

        // If user is authenticated we need to get username.
        if (claims != null) {
            data.setUsername(claims.username);
        }

        appErrorReportService.save(data);

        return httpResponseHelper.getSuccessResponse();
    }
}
